package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.QiniuUtils;
import com.itheima.health.utils.TrashRemovalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private TrashRemovalUtil trashRemovalUtil;
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;



    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealMapper.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealMapper.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealMapper.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 上传文件
     * @param multipartFile
     * @return
     */
    @Override
    public String upload(MultipartFile multipartFile) {
        log.info("文件上传，name:{},size:{}", multipartFile.getOriginalFilename(), multipartFile.getSize());
        //原始文件名
        String originalFileName = multipartFile.getOriginalFilename();
        //使用UUID构造不重复的文件名
        String fileName = System.currentTimeMillis()+"_"+ UUID.randomUUID().toString().replace("-", "") + "_" + originalFileName;
        String res = null;
        //获取输入流并上传
        try (InputStream is = multipartFile.getInputStream()) {
            qiniuUtils.upload2Qiniu(is, fileName);
        } catch (IOException e) {
            log.error("", e);
            return res;
        }

        //构造返回值
        res = qiniuUtils.getUrlPrefix() + fileName;

        //记录又可能删除的图片
        trashRemovalUtil.add(res);

        return res;
    }

    /**
     * 查询全部套餐
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        return setMealMapper.findAll();
    }

    /**
     * 套餐详情
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setMealMapper.findById(id);
        List<CheckGroup> groups = checkGroupMapper.selectBySetmealId(id);

        List<CheckGroup> checkGroups = groups.stream().peek(x -> {
            List<CheckItem> items = checkItemMapper.selectByGroupId(x.getId());
            x.setCheckItems(items);
        }).collect(Collectors.toList());

        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }
}
